import * as api from './api';
import {PAGE_CODE} from './constants';
import {
    getCurrentUserPagePermissionsSuccess,
    getCurrentUserPagePermissionsFail,
    getCurrentUserPagePermissionsApi
} from 'Shared/actions/currentUserContext';
export const ERROR_CODE = 'error';
import {conventTableCondition} from '../utils';
import {message} from 'antd';
import {formatMessage} from './intl';

export const GET_INIT_DATA_BEGIN = 'GET_INIT_DATA_BEGIN';
export const GET_INIT_DATA_SUCCESS = 'GET_INIT_DATA_SUCCESS';
export const GET_INIT_DATA_FAIL = 'GET_INIT_DATA_FAIL';
const getInitDataBegin = () => ({
    type: GET_INIT_DATA_BEGIN
});

const getInitDataSuccess = data => ({
    type: GET_INIT_DATA_SUCCESS,
    data
});
const getInitDataFail = () => ({
    type: GET_INIT_DATA_FAIL
});
/**初始化 */
/*==============================查询列表================================= */
export const GET_LIST_DATA_BEGIN = 'GET_LIST_DATA_BEGIN';
export const GET_LIST_DATA_SUCCESS = 'GET_LIST_DATA_SUCCESS';
export const GET_LIST_DATA_FAIL = 'GET_LIST_DATA_FAIL';
const getListDataBegin = () => ({
    type: GET_LIST_DATA_BEGIN
});
const getListDataSuccess = (condition, data) => ({
    type: GET_LIST_DATA_SUCCESS,
    condition,
    data
});
const getListDataFail = () => ({
    type: GET_LIST_DATA_FAIL
});

export const GET_LIST_MONTH_DATA_SUCCESS = 'GET_LIST_MONTH_DATA_SUCCESS';
const getListMonthDataSuccess = data => ({
    type: GET_LIST_MONTH_DATA_SUCCESS,
    data
});

export const GET_LIST_RATE_DATA_SUCCESS = 'GET_LIST_RATE_DATA_SUCCESS';
const getListRateDataSuccess = data => ({
    type: GET_LIST_RATE_DATA_SUCCESS,
    data
});

export const GET_LIST_COUNTRY_DATA_BEGIN = 'GET_LIST_COUNTRY_DATA_BEGIN';
export const GET_LIST_COUNTRY_DATA_SUCCESS = 'GET_LIST_COUNTRY_DATA_SUCCESS';
export const GET_LIST_COUNTRY_DATA_FAIL = 'GET_LIST_COUNTRY_DATA_FAIL';
const getListCountryDataBegin = () => ({
    type: GET_LIST_COUNTRY_DATA_BEGIN
});
const getListCountryDataSuccess = data => ({
    type: GET_LIST_COUNTRY_DATA_SUCCESS,
    data
});
const getListCountryDataFail = () => ({
    type: GET_LIST_COUNTRY_DATA_FAIL
});

export const GET_LIST_WEEK_DATA_BEGIN = 'GET_LIST_WEEK_DATA_BEGIN';
export const GET_LIST_WEEK_DATA_SUCCESS = 'GET_LIST_WEEK_DATA_SUCCESS';
export const GET_LIST_WEEK_DATA_FAIL = 'GET_LIST_WEEK_DATA_FAIL';
const getListWeekDataBegin = () => ({
    type: GET_LIST_WEEK_DATA_BEGIN
});
const getListWeekDataSuccess = data => ({
    type: GET_LIST_WEEK_DATA_SUCCESS,
    data
});
const getListWeekDataFail = () => ({
    type: GET_LIST_WEEK_DATA_FAIL
});

/*==============================初始化API================================= */
//获取初始化数据和权限
export const getInitData = () => dispatch => {
    dispatch(getInitDataBegin());
    const init = api.getInit().then(res => {
        if(res.ok)
            dispatch(getInitDataSuccess(res.data));
        else
            dispatch(getInitDataFail());
        return res.ok;
    });
    const permissions = getCurrentUserPagePermissionsApi(PAGE_CODE).then(res => {
        if(res.isOk) dispatch(getCurrentUserPagePermissionsSuccess(res.data));
        else dispatch(getCurrentUserPagePermissionsFail(res.statusCode, res.message));
        return res.isOk;
    });

    return Promise.all([init, permissions]).then(values => values[0] && values[1]);
};

/*==============================查询 月action================================= */
//获取查询列表
export const getList = condition => dispatch => {
    const {channel} = condition;
    if(!channel)
        return message.warn(formatMessage({
            id: 'action.query.message1',
            defaultMessage: '查询条件渠道不能为空'
        }));
    dispatch(getListDataBegin());
    const queryCondition = (conventTableCondition(condition));
    const listRate = api.getListRate(queryCondition).then(res => {
        if(res.ok)
            dispatch(getListRateDataSuccess(res.data));
    });
    const listType = api.getListType(queryCondition).then(res => {
        if(res.ok)
            dispatch(getListDataSuccess(condition, res.data));
        else
            dispatch(getListDataFail());
    });
    const listMonth = api.getListMonth(queryCondition).then(res => {
        if(res.ok)
            dispatch(getListMonthDataSuccess(res.data));
    });
    return Promise.all([listRate, listType, listMonth]).then(values => values[0] && values[1] && values[2]);
};

/*==============================查询 国家action================================= */
//获取查询列表
export const getListCountry = condition => dispatch => {
    const {channel} = condition;
    if(!channel)
        return message.warn(formatMessage({
            id: 'action.query.message1',
            defaultMessage: '查询条件渠道不能为空'
        }));
    dispatch(getListCountryDataBegin());
    const queryCondition = conventTableCondition(condition);
    return api.getListCountry(queryCondition).then(res => {
        if(res.ok)
            dispatch(getListCountryDataSuccess(res.data));
        else
            dispatch(getListCountryDataFail());
    });
};

/*==============================查询 周action================================= */
//获取查询列表
export const getListWeek = condition => dispatch => {
    const {channel, monthweek} = condition;
    if(!channel || !monthweek)
        return message.warn(formatMessage({
            id: 'action.query.message2',
            defaultMessage: '查询条件年月不能为空'
        }));
    dispatch(getListWeekDataBegin());
    
    return api.getWeekList({
        dealerId: condition.dealerId,
        channel: condition.channel,
        month: condition.monthweek,
    }).then(res => {
        if(res.ok)
            dispatch(getListWeekDataSuccess(res.data));
        else
            dispatch(getListWeekDataFail());
    });
};
//修改查询条件
export const SAVE_QUERY_CONDITION = 'SAVE_QUERY_CONDITION';
export const saveQueryCondition = (name, value) => ({
    type: SAVE_QUERY_CONDITION,
    name,
    value
});
//重置查询条件
export const RESET_QUERY_PANEL = 'RESET_QUERY_PANEL';
export const resetQueryPanel = () => ({
    type: RESET_QUERY_PANEL,
});

//查询月按钮搜索
export const querySearchMonth = () => (dispatch, getState) => {
    const condition = getState().getIn(['page', 'appState', 'queryCondition']).toJS();
    dispatch(getList(condition));
};
//查询国家按钮搜索
export const querySearchCountry = () => (dispatch, getState) => {
    const condition = getState().getIn(['page', 'appState', 'queryCondition']).toJS();
    dispatch(getListCountry(condition));
};
//查询周按钮搜索
export const querySearchWeek = () => (dispatch, getState) => {
    const condition = getState().getIn(['page', 'appState', 'queryCondition']).toJS();
    dispatch(getListWeek(condition));
};

//选择查询条件
export const SAVE_QUERY_DEALER = 'SAVE_QUERY_DEALER';
export const saveQueryDealer = data => ({
    type: SAVE_QUERY_DEALER,
    data
});
